package com.doublefish.doublefishrobot.ui.home.s5;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.doublefish.doublefishrobot.MainActivity;
import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.ui.home.bean.DeviceBean;
import com.doublefish.doublefishrobot.ui.home.inerface.OnModeMenuRightBtnClickListner;
import com.doublefish.doublefishrobot.utils.ble.bean.ModeConstant;
import com.doublefish.doublefishrobot.utils.socket.DFS5ProtocolMaker;
import com.doublefish.doublefishrobot.utils.socket.SocketManager;
import com.doublefish.doublefishrobot.widget.CommonDialog;


/**
 * 作者:dugaofeng on 2024/10/24 10:10
 */
public class S5ModeMenuFragment extends Fragment implements View.OnClickListener{

    private View view;

    private View backLL;
    private TextView titleTv;
    private TextView rightTv;

    private OnModeMenuRightBtnClickListner onModeMenuRightBtnClickListner;
    private String title = null;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.activty_s5_menu, container, false);
            initViews();
        }
        return view;
    }

    private void initViews() {

        backLL = view.findViewById(R.id.ll_back);
        backLL.setVisibility(View.INVISIBLE);

        titleTv = view.findViewById(R.id.tv_title);
        LinearLayout unloadLL = view.findViewById(R.id.ll_unload);

        DeviceBean deviceBean = SocketManager.getInstance().getCurDeviceBean();
        if (deviceBean != null) {
            title = deviceBean.getAlias();
            if (deviceBean.getDeviceType() == ModeConstant.DeviceTypeS3) {
                unloadLL.setVisibility(View.VISIBLE);
            }
        }

        if (title == null) {
            titleTv.setText(R.string.super_5_robot);
        } else {
            titleTv.setText(title);
        }

        rightTv = view.findViewById(R.id.tv_right);
        rightTv.setText(R.string.disconnect);
        rightTv.setOnClickListener(this);

        TextView normal = view.findViewById(R.id.tv_mode_normal);
        TextView random = view.findViewById(R.id.tv_mode_random);
        TextView train = view.findViewById(R.id.tv_mode_train);
        TextView custom = view.findViewById(R.id.tv_mode_custom);
        TextView unloadTV = view.findViewById(R.id.tv_unload);
        TextView unBindTV = view.findViewById(R.id.tv_unbind_remote);

        normal.setOnClickListener(this);
        random.setOnClickListener(this);
        train.setOnClickListener(this);
        custom.setOnClickListener(this);
        unloadTV.setOnClickListener(this);
        unBindTV.setOnClickListener(this);

        SocketManager.getInstance().setOnDisConnectListner(new SocketManager.OnSocketDisConnectListner() {
            @Override
            public void onDisConnect() {
                toMainActivity();
            }
        });
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_mode_normal:
                toS5ModeNormalActivity();
                break;
            case R.id.tv_mode_random:
                toS5ModeRandomListActivity();
                break;
            case R.id.tv_mode_train:
                toS5ModeTrainListActivity();
                break;
            case R.id.tv_mode_custom:
                toS5ModeCustomListActivity();
                break;

            case R.id.tv_unload:
                showUnloadDialog();
                break;

            case R.id.tv_unbind_remote:
                showUnbindRemoteDialog();
                break;

            case R.id.tv_right:
                onRightTvClick();
                break;
        }
    }

    private void toMainActivity() {
        try {
            Intent intent = new Intent(getActivity(), MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);

            if (onModeMenuRightBtnClickListner != null) {
                onModeMenuRightBtnClickListner.onModeMenuRightBtnClick();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void disConnect() {
        onRightTvClick();
    }

    private void onRightTvClick() {
        //断开socket
        SocketManager.getInstance().disConnectSocket();
    }

    private void toS5ModeNormalActivity() {
        Intent intent = new Intent(getActivity(), S5ModeNormalActivity.class);
        startActivity(intent);
    }

    private void toS5ModeRandomListActivity() {
        Intent intent = new Intent(getActivity(), S5ModeRandomListActivity.class);
        startActivity(intent);
    }

    private void toS5ModeTrainListActivity() {
        Intent intent = new Intent(getActivity(), S5ModeTrainListActivity.class);
        startActivity(intent);
    }

    private void toS5ModeCustomListActivity() {
        Intent intent = new Intent(getActivity(), S5ModeCustomListActivity.class);
        startActivity(intent);
    }

    public void setOnModeMenuRightBtnClickListner(OnModeMenuRightBtnClickListner onModeMenuRightBtnClickListner) {
        this.onModeMenuRightBtnClickListner = onModeMenuRightBtnClickListner;
    }

    private boolean onPauseFlag = false;
    @Override
    public void onResume() {
        super.onResume();
        //获取socket的连接状态
        if (onPauseFlag && !SocketManager.getInstance().didConnected()) {
            toMainActivity();
        }
        onPauseFlag = false;
    }

    @Override
    public void onPause() {
        super.onPause();
        onPauseFlag = true;
    }

    //region 卸球
    private int unloadState = 0;  //0.默认状态 1.当前正在卸球
    private void showUnloadDialog() {
        unloadState = 0;
        String title = getString(R.string.unload_alert);
        String content = getString(R.string.unload_alert_2);
        CommonDialog dialog = new CommonDialog(getActivity(), title, content, new CommonDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (unloadState == 0) {
                    // 默认状态
                    if (confirm) {
                        CommonDialog dialog1 = (CommonDialog)dialog;
                        //修改UI
                        String title2 = getString(R.string.stop_unload_alert);
                        String content2 = getString(R.string.stop_unload_alert_2);
                        dialog1.setTitle(title2);
                        dialog1.setContent(content2);
                        dialog1.setPositiveButton(getString(R.string.stop_unload));
                        dialog1.setShowNegativeButton(false);
                        dialog1.refreshUI();

                        //开始卸球
                        unloadState = 1;
                        startUnload();
                    } else {
                        dialog.dismiss();
                    }
                } else {
                    // 当前正在卸球
                    dialog.dismiss();
                    //停止卸球
                    unloadState = 0;
                    stopUnload();
                }
            }
        });
        dialog.setPositiveButton(getString(R.string.unload));
        dialog.setAutoDismissWhenClick(false);
        dialog.show();
    }

    //开始卸球
    private void startUnload() {
        char[] unloadData = DFS5ProtocolMaker.unload();
        SocketManager.getInstance().sendData(unloadData);
    }

    //停止卸球
    private void stopUnload() {
        char[] stopUnloadData = DFS5ProtocolMaker.stopUnload();
        SocketManager.getInstance().sendData(stopUnloadData);
    }

    //endregion

    //region 遥控器解绑
    private void showUnbindRemoteDialog() {
        String title = getString(R.string.unbind_remote_control);
        String content = getString(R.string.unbind_remote_alert);
        CommonDialog dialog = new CommonDialog(getActivity(), title, content, new CommonDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    unbindRemote();
                }
            }
        });
        dialog.setPositiveButton(getString(R.string.unbind_2));
        dialog.show();
    }

    //遥控器解绑
    private void unbindRemote() {
        char[] unbindData = DFS5ProtocolMaker.unbindRemote();
        SocketManager.getInstance().sendData(unbindData);
    }

    //endregion
}
